package com.fly.system.service.impl;

import com.fly.common.model.SysUserModel;
import com.fly.common.util.UserlUtil;
import com.fly.system.mapper.SysRoleMapper;
import com.fly.system.mapper.SysUserMapper;
import com.fly.system.mapper.SysUserRoleMapper;
import com.fly.system.model.SysRoleModel;
import com.fly.system.model.SysUserRoleModel;
import com.fly.system.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.security.provider.MD5;

import java.util.List;
import java.util.UUID;

/**
 * @CLASSNAME: SysMenuServiceImpl
 * @author: Fly
 * 日期: 2020/6/13  13:17
 * 描述:角色
 **/

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;


    @Override
    public List<SysUserModel> findPage(SysUserModel model) {
        return sysUserMapper.selectList(model);
    }

    @Override
    @Transactional
    public int saveModel(SysUserModel model) {
        String slat = UUID.randomUUID().toString().replaceAll("-","");
        String password = UserlUtil.getDefaultPassword(slat);
        model.setSalt(slat);
        model.setPassword(password);
        UserlUtil.setInsertColumn(model);
        int i = sysUserMapper.insertSelective(model);
        SysUserModel one = sysUserMapper.selectOne(model);
        String roleids = (String) model.getParam().get("roleids");
        if(StringUtils.isNotBlank(roleids)){
            SysUserRoleModel ur = new SysUserRoleModel();
            ur.setUserId(one.getId());
            UserlUtil.setInsertColumn(ur);
            for(String roleId : roleids.split(",")){
                ur.setRoleId(Long.parseLong(roleId));
                i += sysUserRoleMapper.insertSelective(ur);
            }
        }
        return i;
    }

    @Override
    public SysUserModel findModelById(Long id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SysUserRoleModel> findUserRoleListByUserId(Long userId) {
        SysUserRoleModel model = new SysUserRoleModel();
        model.setUserId(userId);
        return sysUserRoleMapper.select(model);
    }

    @Override
    @Transactional
    public int editModelById(SysUserModel model) {
        UserlUtil.setUpdateColumn(model);
        int i = sysUserMapper.updateByPrimaryKeySelective(model);
        String roleids = (String) model.getParam().get("roleids");
        SysUserRoleModel ur = new SysUserRoleModel();
        ur.setUserId(model.getId());
        sysUserRoleMapper.delete(ur);
        if(StringUtils.isNotBlank(roleids)){
            UserlUtil.setInsertColumn(ur);
            UserlUtil.setUpdateColumn(ur);
            for(String roleId : roleids.split(",")){
                ur.setRoleId(Long.parseLong(roleId));
                i += sysUserRoleMapper.insertSelective(ur);
            }
        }
        return i;
    }

    @Override
    public int removeModelById(SysUserModel model) {
        int i = sysUserMapper.delete(model);
        SysUserRoleModel ur = new SysUserRoleModel();
        ur.setUserId(model.getId());
        i += sysUserRoleMapper.delete(ur);
        return i;
    }

    @Override
    public List<SysRoleModel> findRoleAll() {
        return sysRoleMapper.selectAll();
    }
}
